#include "PropertyEditor.h"
#include "Editor.h"
#include "Global.h"

namespace FinalRendering
{
	namespace Editor
	{
		using FinalRendering::Engine::Global;
		using namespace GxRendering::UI;

		PropertyEditor::PropertyEditor(TabPage * page)
		{
			pnlProp = new TitledPanel();
			pnlProp->SetText(L"Properties");
			pnlProp->DockStyle = Control::dsFill;
			page->AddChild(pnlProp);
		
			Container * pnl = new Container();
			pnl->Margin = 8;
			pnl->BorderColor = BS_NONE;
			pnl->DockStyle = Control::dsFill;
			pnlProp->AddChild(pnl);
			
			Container * pnlTop = new Container();
			pnlTop->BorderStyle = BS_NONE;
			pnlTop->SetHeight(30);
			pnlTop->DockStyle = Control::dsTop;
			pnl->AddChild(pnlTop);

			cmbClass = new ComboBox();
			cmbClass->Posit(0,5,224,20);
			for (int i=0; i<Global->GetEntityPluginCount(); i++)
			{
				cmbClass->AddTextItem(Global->GetEntityDesc(i)->ClassName());
			}
			pnlTop->AddChild(cmbClass);

			sheet = new PropertySheet();
			sheet->PickProperty.Bind(this, &PropertyEditor::PickProperty);
			sheet->PropertyChanged.Bind(this, &PropertyEditor::PropertyChanged);
			sheet->DockStyle = Control::dsFill;
			pnl->AddChild(sheet);

		}

		void PropertyEditor::SelectionChanged()
		{
			int typeID = -1;
			entities.Clear();
			sheet->Clear();
			cmbClass->TextBox->SetText(L"");
			for (int i=0; i<WorldEditor->Selection.Count(); i++)
			{
				Selection::ObjectType selType = WorldEditor->Selection[i].Type;
				int id = WorldEditor->Selection[i].ID;
				if (selType == Selection::otBrush)
				{
					entities.Clear();
					sheet->Clear();
					return;
				}
				else
				{
					int tid = WorldEditor->Scene->Entities[id]->TypeID;
					if (typeID != -1 && typeID != tid)
					{
						entities.Clear();
						sheet->Clear();
						return;
					}
					else
					{
						typeID = tid;
						entities.Add(WorldEditor->Scene->Entities[id].operator ->());
					}
				}
			}
			if (entities.Count())
			{		
				for (int i=0; i<entities[0]->Properties.Count(); i++)
				{
					String val = entities[0]->Properties[i]->GetValue();
					bool propFound = true;
					for (int j=1; j<entities.Count(); j++)
					{
						bool found = false;
						for (int k=0; k<entities[j]->Properties.Count(); k++)
						{
							if (entities[j]->Properties[k]->Name == entities[0]->Properties[i]->Name)
							{
								if (entities[j]->Properties[k]->GetValue()!=val)
								{
									val = L"";
									
								}
								found = true;
								goto done;
							}
						}
						if (!found)
						{
							propFound = false;
							break;
						}
					}
done:
					if (!propFound)
						continue;
					PropertyItem * item = new PropertyItem(entities[0]->Properties[i]->Name,
						val,
						entities[0]->Properties[i]->Pickable,
						entities[0]->Properties[i]->ReadOnly);
					sheet->AddPropertyItem(item);
				}
				cmbClass->TextBox->SetText(entities[0]->GetEntityInterface()->GetDesc()->ClassName());
			}
				
		}

		void PropertyEditor::PropertyChanged(String name, String value, bool & succ)
		{
			for (int i=0; i<entities.Count(); i++)
			{
				succ = entities[i]->SetProperty(name, value);
			}
			//WorldEditor->SelectionChanged();
		}

		void PropertyEditor::PickProperty(String name)
		{
			int fid=-1;
			for (int i=0; i<entities[0]->Properties.Count(); i++)
			{
				if (entities[0]->Properties[i]->Name == name)
				{
					fid = i;
					break;
				}
			}
			if (fid !=-1)
			{
				Event<String> e;
				e.Bind(this, &PropertyEditor::PickPropertyResult);
				entities[0]->Properties[fid]->PickValueResult = e;
				propPicking = name;
				entities[0]->Properties[fid]->PickValue();
			}
		}

		void PropertyEditor::PickPropertyResult(String value)
		{
			sheet->SetProperty(propPicking, value);
		}
	}
}